import {Injectable} from "@angular/core";
import {RequestServerHTTPService} from "../services/RequestServerHTTPService";
import {UserFormInfo} from "./UserFormInfo";

/**
 * This class is used to check user's register. Main function is that passes the information to remote server,
 * then deal the response.
 * Created by wubin on 2016-04-18.
 */
@Injectable()
export class UserRegisterHTTPService {
    private _checkUsernameUrl:string = "/register/checkUsername.do";
    private _obtainVerificationResultUrl:string = "/register/obtainAuthResult.do";
    private _storeUserRegisterInfoUrl = "/register/userRegister.do";

    constructor(private _requestServer:RequestServerHTTPService) {
    }

    /**
     * when user input their username, would invoke this method to send username to
     * remote server to check this username's validity.
     * @param username this username that should be check.
     * @param succeedCallback when request succeed will call back this method.
     * @param errorCallback when have some error in current request will callback this method.
     */
    checkUserNameInServer(username:string, succeedCallback:any, errorCallback:any):void {
        this._requestServer.getRequestServerForBack(this._checkUsernameUrl, {username: username}, succeedCallback, errorCallback);
    }

    /**
     * use this method to get the verification's image result.
     * @param succeedCallback when request succeed will call back this method.
     * @param errorCallback when have some error in current request will callback this method.
     */
    obtainVerificationResultInServer(succeedCallback:any, errorCallback:any):void {
        this._requestServer.getRequestServerForBack(this._obtainVerificationResultUrl, null, succeedCallback, errorCallback);
    }

    sendUserRegisterInfo2Server(userFormInfo:UserFormInfo):void {
        this._requestServer.getRequestWithoutBack(this._storeUserRegisterInfoUrl, {
            username: userFormInfo.getUsername,
            password: userFormInfo.getPassword,
            phone: userFormInfo.getPhone
        });
    }
}